package codeGen.core;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import codeGen.core.jpa.JpaControllerGen;
import codeGen.core.jpa.JpaModelGen;
import codeGen.core.jpa.JpaRepositoryGen;
import codeGen.core.model.TableModel;

public class Gen {
    private TableManager tManager;

    public Gen(TableManager tManager) {
        this.tManager = tManager;
    }

    public void launch(Map<String, String> tableAlias, String output, String basePackage, String pagerType) {
        String packagePath = basePackage.replaceAll("\\.", "/");
        //生成文件目录
        File d_root = new File(output);
        if (!d_root.exists()) {
            d_root.mkdirs();
        }
//        File d_data = new File(d_root, "src/" + packagePath + "/data");
//        if (!d_data.exists()) {
//            d_data.mkdirs();
//        }

//        File d_service = new File(d_root, "src/" + packagePath + "/service");
//        if (!d_service.exists()) {
//            d_service.mkdirs();
//        }
//
//        File service_impl = new File(d_service, "impl");
//        if (!service_impl.exists()) {
//            service_impl.mkdirs();
//        }

//        File d_controller = new File(d_root, "src/" + packagePath + "/controller");
//        if (!d_controller.exists()) {
//            d_controller.mkdirs();
//        }


        File d_model = new File(d_root, "src/" + packagePath + "/model");
        if (!d_model.exists()) {
            d_model.mkdirs();
        }


        File d_webview = new File(d_root, "webroot/WEB-INF/view/admin");
        if (!d_webview.exists()) {
            d_webview.mkdirs();
        }

        File jpa_model = new File(d_root, "src/" + packagePath + "/model");
        if (!jpa_model.exists()) {
            jpa_model.mkdirs();
        }

        File jpa_ctrl = new File(d_root, "src/" + packagePath + "/ctrl");
        if (!jpa_ctrl.exists()) {
            jpa_ctrl.mkdirs();
        }

        File jpa_jpa = new File(d_root, "src/" + packagePath + "/jpa");
        if (!jpa_jpa.exists()) {
            jpa_jpa.mkdirs();
        }

        List<String> viewsLinks = new ArrayList<String>(0);
        Set<Entry<String, String>> set = tableAlias.entrySet();
        Iterator<Entry<String, String>> it = set.iterator();
        while (it.hasNext()) {
            Entry<String, String> entry = it.next();
            String table = entry.getKey();
            String alias = entry.getValue();
            //获取表信息
//            TableModel tm = tManager.getTableFieldMap(table);
            TableModel tm = tManager.getTableFieldMap(table, alias);
            tm.setAlias(alias);
            tm.setPagerType(pagerType);
//            mybitis 代码生成
//            //Bean
//            ModelGen.genCode(tm, d_model);
//            //Mapper
//            MapperGen.genCode(tm, d_data);
//            //Service
//            ServiceGen.genCode(tm, d_service);
//            //Controller
//            viewsLinks.add(ControllerGen.genCode(tm, d_controller));
//
//            //View
//            File d_view = new File(d_webview, tm.getAlias());
//            if (!d_view.exists()) d_view.mkdirs();
//            ViewGen.genCode(tm, d_view);

            JpaModelGen.genCode(tm, jpa_model);
            JpaControllerGen.genCode(tm, jpa_ctrl);
            JpaRepositoryGen.genCode(tm, jpa_jpa);

            System.out.printf("table: %s, alias: %s\n", table, alias);

        }
        //输出view/admin/index.jsp中菜单配置
        for (String link : viewsLinks) {
            System.out.println(link);
        }
    }


}
